#!/sbin/runscript
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/app-admin/syslog-ng/files/syslog-ng.rc6.3.3,v 1.2 2011/10/19 06:19:38 mr_bones_ Exp $

extra_commands="checkconfig"
extra_started_commands="reload"

description_checkconfig="Check the configuration file that will be used by \"start\""
description_reload="Reload the configuration without exiting"

SYSLOG_NG_SERVICE=${SVCNAME#*.}
SYSLOG_NG_SERVICE=${SYSLOG_NG_SERVICE:-syslog-ng}

SYSLOG_NG_CONFIGFILE=${SYSLOG_NG_CONFIGFILE:-/etc/syslog-ng/${SYSLOG_NG_SERVICE}.conf}
SYSLOG_NG_PIDFILE=${SYSLOG_NG_PIDFILE:-/var/run/${SVCNAME}.pid}
SYSLOG_NG_OPTS="--cfgfile ${SYSLOG_NG_CONFIGFILE} --pidfile ${SYSLOG_NG_PIDFILE} ${SYSLOG_NG_OPTS}"

depend() {
	# Make networking dependency conditional on configuration
	if [ -f "${SYSLOG_NG_CONFIGFILE}" ]; then
		case `sed 's/#.*//' "${SYSLOG_NG_CONFIGFILE}"` in
			*source*tcp*|*source*udp*|*destination*tcp*|*destination*udp*)
				need net
				use stunnel ;;
		esac
	fi

	config "${SYSLOG_NG_CONFIGFILE}"
	use clock
	need hostname localmount
	provide logger
}

checkconfig() {
	if [ ! -e "${SYSLOG_NG_CONFIGFILE}" ] ; then
		eerror "You need to create ${SYSLOG_NG_CONFIGFILE} first."
		eerror "An example can be found in /usr/share/doc/syslog"

		return 1
	fi
	syslog-ng -s -f "${SYSLOG_NG_CONFIGFILE}"

	# the start and reload functions have their own eends so
	# avoid calling this twice when there are no problems
	[ $? -eq 0 ] || eend $? "Configuration error. Please fix your configfile (${SYSLOG_NG_CONFIGFILE})"
}

start() {
	checkconfig || return 1
	ebegin "Starting ${SVCNAME}"
	[ -n "${SYSLOG_NG_OPTS}" ] && SYSLOG_NG_OPTS="-- ${SYSLOG_NG_OPTS}"
	start-stop-daemon --start --pidfile "${SYSLOG_NG_PIDFILE}" --exec /usr/sbin/syslog-ng ${SYSLOG_NG_OPTS}
	eend $? "Failed to start ${SVCNAME}"
}

stop() {
	ebegin "Stopping ${SVCNAME}"
	start-stop-daemon --stop --pidfile "${SYSLOG_NG_PIDFILE}"
	eend $? "Failed to stop ${SVCNAME}"
	sleep 1 # needed for syslog-ng to stop in case we're restarting
}

reload() {
	if [ ! -f "${SYSLOG_NG_PIDFILE}" ]; then
		eerror "${SVCNAME} isn't running"
		return 1
	fi
	checkconfig || return 1
	ebegin "Reloading configuration and re-opening log files"
	start-stop-daemon --signal HUP --pidfile "${SYSLOG_NG_PIDFILE}"
	eend $?
}
